﻿@model PrjAssessModels.BaseModels.Project
@{
    ViewBag.Title = "工作量考核";
}


<div data-ng-app="myapp" data-ng-controller="myCtrl">
    <br />
    <div class="container">

        <div class="panel panel-default">
            <div class="panel-heading">
                <h5>工作量考核</h5>
            </div>
            <div class="panel-body">

                <form class="form-horizontal" role="form">
                    <div class="form-group">
                        <label for="起点时间" class="col-sm-2 control-label" >起点时间</label>
                        <div class="col-sm-10">
                            <input type="date" name="起点时间" class="form-control" data-ng-model="startTime" 
                                data-ng-bind="startTime" data-ng-disabled="!startRangeEnable">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="起点进度" class="col-sm-2 control-label">起点进度:{{startValue}}%</label>
                        <div class="col-sm-10">
                            <input type="range"  id="startRange" name="起点进度" data-ng-model="startValue"
                                data-ng-bind="startValue" min="0" max="100"
                                style="width:100%;" data-ng-disabled="!startRangeEnable">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="终点时间" class="col-sm-2 control-label" >终点时间</label>
                        <div class="col-sm-10">
                            <input type="date" name="终点时间" data-ng-model="endTime" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="终点进度" class="col-sm-2 control-label">终点进度:{{endValue}}%</label>
                        <div class="col-sm-10">
                            <input type="range" name="终点进度" max="100" min="{{startValue}}" style="width:100%;" data-ng-model="endValue">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <a class="btn btn-primary" data-ng-click="add()" data-ng-disabled="!canAdd">提 交</a>
                        </div>
                    </div>
                </form>
                <hr />
      
                <div>
                    <div class="list-group">
                        <table class="table">
                            <thead>
                                <tr>
                                   <th>考核起始时间</th>
                                   <th>考核结束时间</th>
                                   <th>考核起始百分比</th>
                                   <th>考核结束百分比</th>
                                   <th>班组额度</th>
                                   <th>项目部额度</th>
                                   <th>*</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr data-ng-repeat="item in assessList1" data-ng-click="clickItem(item)">
                                    <th>{{item.startTime}}</th>
                                    <th>{{item.Time}}</th>
                                    <th class="text-danger">{{item.startValue}}%</th>
                                    <th class="text-danger">{{item.Value}}%</th>
                                    <th>{{item.teamWorkSum}}￥</th>
                                    <th>{{item.companyWorkSum}}￥</th>
                                    <th><a href="#" data-ng-click="delete(item)">删除</a></th>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>


        </div>
        <hr />
        <div class="panel panel-default" id="chartParent">
            <div class="panel-heading">
                <h5>考核结果</h5>
            </div>
            <div id="chart">
            </div>
        </div>
    </div>

</div>

<script type="text/javascript">
    var myapp = angular.module('myapp', []);
    myapp.controller('myCtrl', ['$scope', '$http', function ($scope,$http) {

        $scope.value = 0;
        $scope.time = '@DateTime.Now.ToString("yyyy-MM-dd")';
        $scope.startTime = $scope.time;
        $scope.endTime = $scope.time;
        $scope.prjId = '@ViewBag.PrjId';
        $scope.title = '工作量考核';
        $scope.assessList = [];
        $scope.showDetails = false;
        $scope.firstRun = true;
        $scope.used = 0, $scope.left = 0;
        $scope.startRangeEnable = false;
        $scope.canAdd = false;
        
        var msg = JSON.parse('@Html.Raw(ViewBag.msg)');
        
        var createChart = function () {
            var pts = [];
            angular.forEach($scope.assessList, function (item)
            {
                var pt = {
                    x: item.Time.toString(),
                    y: item.Value,
                    name: "工作量考核"
                };
                pts.push(pt);
            });

            if ($scope.firstRun === false) {
                try{
                    $('#chart').ejChart('destroy');
                    $('#chart').remove();
                    var chart = document.createElement("div");
                    chart.id = "chart";
                    document.getElementById("chartParent").appendChild(chart);
                }
                catch (err) {
                }
    
            }
            $scope.firstRun = false;
            if ($scope.assessList.length === 0) return;
            $('#chart').ejChart({
                chartAreaBorder: {
                    visible: false
                },
                primaryXAxis: {
                    title: {
                        text: "日期"
                    }
                },
                primaryYAxis: {
                    labelFormat: "{value}%",
                    title: {
                        text: '工作量考核'
                    }
                },
                series: [{
                    points: pts,
                    name: '工作量考核', type: 'Area',
                    animation: true,

                    style: { borderColor: 'transparent', opacity: 0.5, interior: '#69D2E7' }

                }],
                showTooltip: false,
                load: "loadTheme",
                text: '工作量考核',
                size: {
                    height: 350
                },
                legend: {
                    visible: false
                }
            });
        }

        $scope.freshData = function () {
            $http({
                url: '@Url.Action("GetAssessItems","DoAssess")',
                method: 'get',
                params: { prjId: $scope.prjId, title: '工作量考核' }
            }).success(function (data) {
                data.forEach(function (item, index) {
                    item.Value = Math.floor(item.Value * 100);
                    item.Time = item.Time.substring(0, 10);
                    if (index > 0) {
                        //增加两个属性
                        item.startTime = data[index - 1].Time;
                        item.startValue = data[index - 1].Value;
                        item.teamWorkSum = Math.floor((data[index].Value - data[index - 1].Value) / 100 * msg.workSum * msg.paraTeam);
                        item.companyWorkSum = Math.floor((data[index].Value - data[index - 1].Value) / 100 * msg.workSum * msg.paraCompany);

                    }
                });
                $scope.assessList = data;
                $scope.assessList1 = data.slice(1);

                var rangeInput  = document.getElementById('startRange');
                if (data.length === 0)
                {
                    rangeInput.min = 0; rangeInput.max = 100; rangeInput.value = 0;
                    $scope.startRangeEnable = true;
                    $scope.startValue = 0; 
                } else
                {
                    $scope.startRangeEnable = false;
                    $scope.startValue = data[data.length - 1].Value;
                    $scope.startTime = data[data.length - 1].Time;
                }
                $scope.endValue = $scope.startValue;
                createChart();
            });
         };
        $scope.freshData();//显示数据
        

        $scope.delete = function (item) {
            $scope.selectedItem = item;
            if ($scope.startRangeEnable == true) return;
            if ($scope.assessList.length == 2) {
                deleteItem($scope.assessList[0]);
                deleteItem($scope.assessList[1], function () {
                    $scope.freshData();
                });
            }
            else {
                deleteItem($scope.selectedItem, function () {
                    $scope.freshData();
                });
            }
        }

        $scope.$watch(function () {
            var time1 = Date.parse($scope.startTime);
            var time2 = Date.parse($scope.endTime);

            if (time2 - time1 <= 0 || $scope.startValue >= $scope.endValue) {

                return false;
            }
            else {
                return true;
            }
        }, function (newValue) {
            $scope.canAdd = newValue;
        });


        $scope.add = function () {

            var array = [];
            if ($scope.startRangeEnable == true) {
                array.push({ PrjId: $scope.prjId, Title: '工作量考核', SubTitle: '工作量考核', Time: $scope.startTime, Value: $scope.startValue/100 },
                    { PrjId: $scope.prjId, Title: '工作量考核', SubTitle: '工作量考核', Time: $scope.endTime, Value: $scope.endValue/100 });
            } else {
                array.push({ PrjId: $scope.prjId, Title: '工作量考核', SubTitle: '工作量考核', Time: $scope.endTime, Value: $scope.endValue/100 });
            }
            addItemsToDataBase(array);
        };
        //帮助方法,删除一个元素
        function deleteItem(item,callback) {
            $http({
                url: '@Url.Action("deleteAssessItem","DoAssess")',
                method: 'post',
                data: { jsonStr: JSON.stringify(item) }
            }).success(function (data) {
                $scope.selectedItem = null;
                if (callback) {
                    callback();
                }
            });
        }
        //帮助方法,存入数据库=_=,json 数组格
        function addItemsToDataBase(array) {
            $http({
                url: '@Url.Action("insertAssessItemArray","DoAssess")',
                method: 'post',
                data: {
                    jsonArray: JSON.stringify(array)
                }
            }).success(function (data) {
                //成功
                $scope.freshData();
                console.log("成功的插入json数组!!");
            });
        }
    }]);
</script>
